package com.study.d8_collection_test;

import java.util.*;

public class Room {
    //准备一副牌
    private ArrayList<Card> cards = new ArrayList<>();

    //初始化54张牌
    {
        //准备点数
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10",
                "J", "Q", "K", "A", "2"};
        //准备花色
        String[] colors = { "♦","♣", "♥","♠"};
        int count=0;
        for (String number : numbers) {
            for (String color : colors) {
                Card card = new Card(number, color,++count);
                cards.add(card);
            }
        }
        cards.add(new Card("", "🃏",++count));
        cards.add(new Card("", "👲",++count));
        System.out.println("新牌:" + cards);
    }

    public void start() {
        //洗牌
        Collections.shuffle(cards);
        System.out.println("洗牌后：" + cards);
        //发牌
        List<Card> player1 = new ArrayList<>();
        List<Card> player2 = new ArrayList<>();
        List<Card> player3 = new ArrayList<>();
        for (int i = 0; i < cards.size() - 3; i++) {
            int mod = i % 3;
            switch (mod) {
                case 0:
                    player1.add(cards.get(i));
                    break;
                case 1:
                    player2.add(cards.get(i));
                    break;
                case 2:
                    player3.add(cards.get(i));
                    break;
            }
        }

        //地主牌
        List<Card> diZhuCards = cards.subList(cards.size() - 3, cards.size());
        //抢地主
        int diZhu = getDiZhu();
        System.out.println("地主是p" + diZhu);
        switch (diZhu) {
            case 1:
                player1.addAll(diZhuCards);
                break;
            case 2:
                player2.addAll(diZhuCards);
                break;
            case 3:
                player3.addAll(diZhuCards);
                break;

        }
        //对牌排序
        sortCards(player1);
        sortCards(player2);
        sortCards(player3);
        //看牌
        System.out.println("player1的牌："+player1);
        System.out.println("player2的牌："+player2);
        System.out.println("player3的牌："+player3);

    }

    public int getDiZhu() {
        int dizhu = 0;
        double ran = Math.random() * 10000;
        long round = Math.round(ran);
        long firstHand = round % 3;
        int prior = 0;
        if (firstHand == 0) firstHand = 3;
        Scanner sc = new Scanner(System.in);
        System.out.println("p" + firstHand + "是否叫地主，请输入：1叫 2不叫");
        int input1 = sc.nextInt();
        if (input1 == 1)
            prior = (int) firstHand;
        int secondHand;
        if (firstHand == 3) {
            secondHand = 1;
        } else {
            secondHand = (int) (firstHand + 1);
        }
        int input2 = 0;
        if (input1 == 1) {
            System.out.println("p" + secondHand + "是否抢地主，请输入：1抢 2不抢");
            input2 = sc.nextInt();
        } else if (input1 == 2) {
            System.out.println("p" + secondHand + "是否叫地主，请输入：1叫 2不叫");
            input2 = sc.nextInt();
            if (input2 == 1)
                prior = secondHand;
        }

        int thirdHand;
        if (secondHand == 3) {
            thirdHand = 1;
        } else {
            thirdHand = secondHand + 1;
        }
        int input3 = 0;
        if (input1 == 2 && input2 == 2) {
            System.out.println("p" + thirdHand + "是否叫地主，请输入：1叫 2不叫");
            input3 = sc.nextInt();
            if (input3 == 1)
                prior = thirdHand;
            if (input3 == 2) {
                // return;
            }
        } else if (input1 == 1 && input2 == 1 || input1 == 2 && input2 == 1 || input1 == 1 && input2 == 2) {
            System.out.println("p" + thirdHand + "是否抢地主，请输入：1抢 2不抢");
            input3 = sc.nextInt();
        }

        int[] arr2 = {0, (int) firstHand, secondHand, thirdHand};
        int[] intputArr = {0, input1, input2, input3};
        if (input1 == 2 && input2 == 2 || input2 == 2 && input3 == 2 || input1 == 2 && input3 == 2) {
            for (int i = 1; i < intputArr.length; i++) {
                if (intputArr[i] == 1) {
                    dizhu = arr2[i];
                    return dizhu;
                }
            }
        }
        if (input1 == 1 && input2 == 1 || input2 == 1 && input3 == 1 || input1 == 1 && input3 == 1) {
            System.out.println("p" + prior + "是否抢地主，请输入：1抢 2不抢");
            int input4 = sc.nextInt();
            if (input4 == 1) {
                dizhu = prior;
            } else if (input4 == 2) {
                for (int i = intputArr.length-1; i >0; i--) {
                    if (intputArr[i] == 1)
                        return arr2[i];
                }
            }
        }
        return dizhu;
    }
    public void sortCards(List<Card> cards){
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getWeight()-o2.getWeight();
            }
        });
    }
}
